import pymysql

from dumpDB.items import DumpfileItem

conn = pymysql.connect(host='localhost', port=3306, db='mysql', user='root', password='153838', charset='utf8')
cursor = conn.cursor()

cursor.execute('''create table if not exists ai_db.news(
                    id int auto_increment primary key,
                    name varchar(100) unique,
                    keyword varchar(100)
                    )engine=innodb
               ''')
conn.commit()


def inser_table(db_table_name, obj: DumpfileItem):
    name = str(obj['name'])
    keyword = str(obj['keyword'])
    sql = f'replace into {db_table_name}(name, keyword) values({repr(name)}, {repr(keyword)})'
    print(sql)
    cursor.execute(sql)
    conn.commit()


def close_conn():
    cursor.close()
    conn.close()
